# module name builtins.py
# $Id: builtins.py ddcad87fab9c 2013/08/17 19:20:30 pgurumur $
# Copyright (c) 2013 Prabhu Gurumurthy <pgurumur@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# File builtins.py generated on Sun, July 21, 2013 23:01:05 PM
#
__version__  = "$Revision: ddcad87fab9c $"
__author__   = "$Author: pgurumur $"
__modified__ = "$Date: 2013-08-17 19:20:30Z $"

try:
   from . import exception, stdlib, base
except ImportError:
   import exception, stdlib, base

class BerylPrint(base.BerylFunction):
   def __init__(self, Tokens = []):
      super(BerylPrint, self).__init__("print")
      self._builtin = True
      self._tokens = Tokens
      self._vars = []
      self._nodes = []

   def _print(self, token):
      value = None
      if isinstance(token, base.BerylNode):
         value = token.value
      elif token in self.parent.variable:
         varnode = self.parent.objects[token]
         value = varnode.value
      else:
         self.error("undefined variable %s at line %d", token, self.line)

      if value:
         print(value, "", end = "")

   def tovalue(self, tokens):
      for item in tokens:
         if isinstance(item, base.BerylNode):
            self._nodes.append(item.value)
         elif item in self.parent.variable:
            varnode = self.parent.objects[item]
            self._vars.append(varnode.value)

   def _rprint(self, tokens):
      for item in tokens:
         self._print(item)

   def __call__(self, *args, **kwargs):
      toklen = len(self._tokens)
      if toklen == 1:
         node = self._tokens[0]
         output = None
         if isinstance(node, base.BerylNode):
            output = node.value
         elif isinstance(node, str):
            node = self.parent.find(node)
            if node:
               output = node.value
            else:
               self.error("unknown variable in line %d", self.line)

         print(output)
      elif toklen > 1:
         varlist = []
         for item in self._tokens[1:]:
            temp = self.parent.find(item)
            if temp:
               varlist.append(temp.value)

         mesg = None
         try:
            mesg = stdlib.sprintf(self._tokens[0].value, *varlist)
         except TypeError as err:
            self._rprint(self._tokens)
         else:
            print(mesg)
      elif toklen == 0:
         print()
